Adding a directory tree function

back to section start!
  Here's a short script I knocked up after being inspired by someone else,
you know who you are ;-)

  If any of you out there had Directory Opus V4, then you'd know you were
able to display a 'tree' of all the directories for a particular drive.  You
could click on one to have that directory displayed.

  While not as aesthetically pleasing as the version Jon incorporated into
DO4, this version does the same basic thing, that is, it displays a tree of
directories for a particular device and allows you to go to one by double
clicking on it.

  1/*
  2$VER: DirTree.dopus5 0.2 (7.10.98)
  3
  4Call as:  ARexx  DOpus5:ARexx/DirTree.dopus5 [REGEN] <Device>
  5
  6          Flags  Run Async
  7
  8  where:  REGEN  = causes a new directory tree file to be created.
  9          Device = the device, eg. HD0:, SD0:, DF0:, etc
 10
 11Example:  DirTree.dopus5 HD0:
 12
 13*/
 14options results
 15parse arg regen device
 16if device = '' then device = regen
 17regen = regen ~= device
 18
 19address 'DOPUS.1'
 20if ~show('l','rexxsupport.library') then
 21  call addlib('rexxsupport.library',0,-30)
 22
 23device = strip(strip(device,,'"'))
 24oldcd = pragma('d',device)
 25device = pragma('d',oldcd)
 26if right(device,1) ~= ':' then do
 27  dopus request '"Specify DEVICE only, eg. ''HD0:''" OK'
 28  exit
 29  end
 30tfile = left(device,pos(':',device))||'.dirtree'
 31if regen then call delete(tfile)
 32
 33if ~exists(tfile) then do
 34  address command 'List 'device' DIRS ALL LFORMAT "%P%N" >T:dirtree.temp'
 35  address command 'Sort T:dirtree.temp 'tfile
 36  'command protect 'tfile' set H'
 37  call delete('T:dirtree.temp')
 38  end
 39
 40if ~open('dirtree',tfile,'r') then do
 41  dopus request '"ERROR: Unable to open ''tree'' file." OK'
 42  exit
 43  end
 44else do
 45  totlines = 0
 46  do while ~eof('dirtree')
 47    call readln('dirtree')
 48    totlines = totlines + 1
 49  end
 50  call seek('dirtree',0,'b')
 51  nlength = length(totlines)
 52  end
 53
 54lister new invisible mode name
 55handle = result
 56lister set handle field off
 57lister set handle toolbar
 58lister set handle busy on
 59info.NAME = copies('0',nlength)
 60info.DISPLAY = device
 61lister addstem handle info.
 62lister refresh handle full
 63lister set handle value info.NAME device
 64lister set handle visible on
 65
 66linenum = 1
 67do while ~eof('dirtree')
 68  dirline = readln('dirtree')
 69  path = dirline
 70  if pos(':',dirline) ~= 0 then parse var dirline dev ':' dirline
 71  if dirline ~= '' then do
 72    linenum = linenum + 1
 73    if pos('/',dirline) ~= 0 then do
 74      howmany = 0
 75      do while pos('/',dirline) ~= 0
 76        parse var dirline fore'/'dirline
 77        howmany = howmany + 1
 78      end
 79      dirline = copies(' ',3 * howmany)||dirline
 80      end
 81    dirline = '   '||dirline
 82    do i = 1 to length(dirline) by 3
 83      if substr(dirline,i + 3,1) ~= ' ' | substr(dirline,i + 4,1) ~= ' ' then do
 84        dirline = overlay('+--',dirline,i)
 85        leave
 86        end
 87    end
 88    info.NAME = right(linenum,nlength,'0')
 89    info.DISPLAY = dirline
 90    lister addstem handle info.
 91    lister refresh handle
 92    lister set handle value info.NAME path
 93    end
 94end
 95call close('dirtree')
 96lister set handle busy off
 97
 98call openport('DirTree-handler')
 99lister set handle handler 'DirTree-handler'
100
101do until event = 'inactive'
102  if waitpkt('DirTree-handler') then do
103    packet = getpkt('DirTree-handler')
104    if packet ~= '00000000'x then do
105      event = getarg(packet,0)
106      name = getarg(packet,2)
107      if event = 'doubleclick' then do
108        lister query handle value name
109        path = result
110        lister set handle handler
111        lister set handle toolbar toolbar
112        lister wait handle quick
113        lister read handle path force
114        end
115    call reply(packet,0)
116    end
117end
118
119call closeport('DirTree-handler')
120exit

  When run for the first time on a device, there will be a delay while every
directory is listed and then sorted.  This file is then save to the root of
the device for quick access next time, it will be named as
'.dirtree', and will have the 'h' protection bit set so as to not
be displayed if you have the 'Hidden' option set for your listers.

  Some highlights of the script are:

Lines:  54 - 64  We open a new lister initially invisible while we set up
                 it's toolbar and field titles to off.  We set it's state
                 to busy, add the first entry (the device), then refresh it
                 and make it visible.
        66 - 87  These lines just read the file a line at a time and format
                 them ready for the lister.
        88 - 92  We set the compound variable info.NAME to equal the line
                 number in the file, (so each entry is individual), set the
                 reformatted text to the compound variable info.DISPLAY and
                 add them to the lister display using the  lister addstem 
                 command.  Because we have specified a DISPLAY field this
                 will be what is displayed in the lister.  Then we associate
                 the NAME field with a value, in this case the path of the
                 directory using the  lister set value  command.
       101 - 117 This is our custom handler routine which just waits for a
                 doubleclick event to happen.  When it does, we query the
                 value of the name/value pair we made previously, this gives
                 us back the path for that entry.  Set the handler to null,
                 this causes the handler for this lister to become inactive
                 and thus terminate.  Set the toolbar to the default toolbar,
                 usually called 'toolbar' in the DOpus5:Buttons directory.
                 Wait until the lister is idle, and then force the lister to
                 read in the requested path.

  All very simple :)

  Clicking this  button  will copy it to your DOpus5:ARexx/ directory.

DOpus PLUS - giving you that bit extra...